package leetcode

func convertMethod1(s string, numRows int) string {
	if numRows == 1 {
		return s
	}

	table := make([][]int32, numRows)
	r := ""

	for i := 0; i < numRows; i++ {
		table[i] = make([]int32, len(s))
	}

	x, y := 0, -1
	change := false

	for _, v := range s {
		if change {
			x++
			y--
		} else {
			y++
		}
		table[y][x] = v

		if y == 0 {
			change = false
		}
		if y+1 == numRows {
			change = true
		}
	}

	for i := 0; i < numRows; i++ {
		for j := 0; j < len(s); j++ {
			if table[i][j] == 0 {
				continue
			}
			r += string(table[i][j])
		}
	}

	return r
}

func convertMethod2(s string, numRows int) string {
	if numRows == 1 {
		return s
	}
	r := make([]int32, len(s))

	var table [][]int32
	var row []int32

	x, y := -1, 0
	change := false

	for _, v := range s {
		if change {
			row = make([]int32, numRows)
			table = append(table, row)
			x--
			y++
		} else {
			x++
		}
		if x == 0 {
			change = false
			row = make([]int32, numRows)
			table = append(table, row)
		}
		if x+1 == numRows {
			change = true
		}
		table[y][x] = v
	}

	n := 0
	for i := 0; i < numRows; i++ {
		for j := 0; j < len(table); j++ {
			if table[j][i] == 0 {
				continue
			}
			r[n] = table[j][i]
			n++
		}
	}

	return string(r)
}

//func convertMethod2(s string, numRows int) string {
//	width := (len(s) + numRows - 1) / numRows
//	table := make([][]int32, numRows, width)
//
//	x, y, n := 0, 0, 1
//
//	for k, v := range s {
//
//		if k < (2*n)*(numRows-1) && k > (2*n-1)*(numRows-1) {
//
//		}
//	}
//step := numRows - 2
//width := (len(s) + numRows - 1) / numRows
//table := make([][]int32, numRows, width)
//
//x, y, n := 0, 0, 1
//change := false
//
//for k, v := range s {
//table[x][y] = v
//if k < numRows * n + step*(n-1) {
//y++
//}
//if y % numRows == 0 {
//n++
//change = true
//}
//if change {
//x++
//y--
//}
//if x == 0 {
//change = false
//}

//}
//
//	return ""
//}
